package com.evanflare.domain;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

/**
 * 复习文档的元信息
 * @author Yanion_gwgzh
 */
public class ReviewDocumentInfo implements Comparable<ReviewDocumentInfo>{
    /**
    1h，3h，1天，3天，7天，21天，30天，90天，180天，365天，1030天。
     */
    public static final int[] TYPE1_REVIEW_TIME_INTERVAL = {1,3,24,3*24,7*24,21*24,30*24,90*24,180*24,365*24,1030*24};
    private String name;
    private String path;
    private int type;
    private int reviewTimes;
    private LocalDateTime lastReviewTime;
    private LocalDateTime nextReviewTime;
    private final long overReviewHours;
    public ReviewDocumentInfo(String name,
                              String path,
                              int type,
                              int reviewTimes,
                              LocalDateTime lastReviewTime){
        // 1 赋值
        this.name = name;
        this.path = path;
        this.type = type;
        this.reviewTimes = reviewTimes;
        this.lastReviewTime = lastReviewTime;
        // 2 计算属性
        // 2.1 计算下次复习时间
        if(type == 0){
            // 复习时间为1h，12h，1h，12h无限循环
            nextReviewTime = lastReviewTime.plusHours(reviewTimes%2==0?1:12);
        }else if(type == 1){
            if(reviewTimes >= TYPE1_REVIEW_TIME_INTERVAL.length){
                this.type = -1;
                nextReviewTime = LocalDateTime.MAX;
            }else{
                nextReviewTime = lastReviewTime.plusHours(TYPE1_REVIEW_TIME_INTERVAL[reviewTimes]);
            }
        }else {
            // type = -1
            nextReviewTime = LocalDateTime.MAX;
        }
        // 2.2 计算已经超时的 hours, 当前时间减去复习时间在转换单位为hour
        overReviewHours = (Instant.now().getEpochSecond() - lastReviewTime.toEpochSecond(OffsetDateTime.now().getOffset()))/(60*60);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getType() {
        return type;
    }
    public long getOverReviewHours() {
        return overReviewHours;
    }
    public void setType(int type) {
        this.type = type;
    }

    public int getReviewTimes() {
        return reviewTimes;
    }

    public void setReviewTimes(int reviewTimes) {
        this.reviewTimes = reviewTimes;
    }

    public LocalDateTime getLastReviewTime() {
        return lastReviewTime;
    }

    public void setLastReviewTime(LocalDateTime lastReviewTime) {
        this.lastReviewTime = lastReviewTime;
    }

    public LocalDateTime getNextReviewTime() {
        return nextReviewTime;
    }

    public void setNextReviewTime(LocalDateTime nextReviewTime) {
        this.nextReviewTime = nextReviewTime;
    }

    @Override
    public String toString() {
        return "ReviewDocumentInfo{" +
                "name='" + name + '\'' +
                ", path='" + path + '\'' +
                ", type=" + type +
                ", reviewTimes=" + reviewTimes +
                ", lastReviewTime=" + lastReviewTime +
                ", nextReviewTime=" + nextReviewTime +
                ", overReviewHours=" + overReviewHours +
                '}';
    }

    @Override
    public int compareTo(ReviewDocumentInfo o) {
        int res = 0;
        res += (o.reviewTimes - this.reviewTimes) * 1030*24;
        res += (int) (this.overReviewHours - o.overReviewHours);
        return -res;
    }
}
